package.cpath = "luaclib/?.so;3rd/lua-cjson/?.so;"
local skynet = require "skynet" 
local json  = require "cjson"
 

FRIENDS = {} 
local FRIENDS_room = {}  --房间room_type总人数 
local FRIENDS_room_id_max = {}  --房间id max 
 
local FRIENDS_KEY = {}  
FRIENDS_KEY.key = 'friends_msg_'      --朋友圈新增  

function FRIENDS.get_version( )  
    local json_str = skynet.call("MG_SAVE", "lua", "get_version" ) 
    local data = {}
    if json_str ~= false then
        data = json.decode(json_str)
    end
    return data
end

-- 得到我加入的 朋友圈
--fid  朋友圈id  
--name  朋友圈名称   
--uid  玩家id   
--flag  玩家标示  1为管理员 2为成员 3为申请还未批准成员  
function FRIENDS.get_uid(uid) 
    local sql = string.format("select * from  `friends` where flag in (1,2) and uid = %d ",tonumber(uid))
    local data = skynet.call("MG_SAVE", "lua", "mysql_get_all", sql ) 
    return data
end
  
--   得到 朋友圈 信息
--fid  朋友圈id  
--name  朋友圈名称  
--uid  玩家id  
--flag  玩家标示  1为管理员 2为成员 3为申请还未批准成员 
function FRIENDS.get_fid_one(fid) 
    local sql = string.format("select * from  `friends` where flag in (1,2) and fid = %d ",tonumber(fid)) 
    local data = skynet.call("MG_SAVE", "lua", "mysql_get_one", sql ) 

    return data
end
function FRIENDS.get_fid_admin(fid) 
    local sql = string.format("select * from  `friends` where flag = 1 and fid = %d ",tonumber(fid))
    local data = skynet.call("MG_SAVE", "lua", "mysql_get_all", sql ) 
    return data
end
function FRIENDS.get_fid(fid) 
    local sql = string.format("select distinct uid  from  `friends` where flag in (1,2) and fid = %d ",tonumber(fid))

    local data = skynet.call("MG_SAVE", "lua", "mysql_get_all", sql ) 
    return data
end
-- 申请 加入 朋友圈
function FRIENDS.add(uid,fid)   
    local sql = string.format("select * from  `friends` where flag = 1 and fid = %d",tonumber(fid))
    local data = skynet.call("MG_SAVE", "lua", "mysql_get_one", sql ) 
    if data ~= false then
        local sql = string.format("select * from  `friends` where uid = %d  and fid = %d ",tonumber(uid),tonumber(fid))
        local qu = skynet.call("MG_SAVE", "lua", "mysql_get_one", sql ) 
        if qu == false then
            local sql = string.format("insert into `friends` values (0,%d,3,'%s',%d) ",tonumber(uid),data.name,tonumber(fid))
            local rs = skynet.call("MG_SAVE", "lua", "mysql_insert", sql ) 
            return rs
        elseif qu.flag == 3 then
            return -2 
        end
        return -1
    end
    return 0
end

 --  
function FRIENDS.update(uid,fid ) 
    local sql = string.format("update  `friends` set flag = 2 where uid = %d  and fid = %d ",tonumber(uid),tonumber(fid))

    local rs = skynet.call("MG_SAVE", "lua", "mysql_update", sql ) 
end
 --  
function FRIENDS.del(uid,fid ) 
    local sql = string.format("delete from  `friends` where uid = %d  and fid = %d ",tonumber(uid),tonumber(fid))

    local rs = skynet.call("MG_SAVE", "lua", "mysql_update", sql ) 
end

 -- 获取朋友圈消息
function FRIENDS.get_msg(uid  )  
    local str = skynet.call("MG_SAVE", "lua", "redis_lrange", FRIENDS_KEY.key .. uid,0,20 ) 
    local data = {} 
    local arr = {} 
    local old_room_id = {} 
    local now_time = os.time() - 3600 * 2 --  个小时以上数据不处理
    if str ~= nil then
        -- print('===============get_msg==================',#str)
        local data_key = 1
        for k,v in pairs(str) do
            arr = json.decode(v) 
            if arr['time'] < now_time then 
                if k > 1 then   --删除过期数据 
                    local rs = skynet.call("MG_SAVE", "lua", "redis_ltrim", FRIENDS_KEY.key .. uid,0,k-2 ) 
                else  
                    local rs = skynet.call("MG_SAVE", "lua", "redis_del", FRIENDS_KEY.key .. uid ) 
                end
                break
            end 
            local room_id = tonumber(arr['room_id'])
            if old_room_id[room_id] == nil then 
                old_room_id[room_id] = 1
                data[data_key] = arr 
                data_key = data_key + 1
            end 
            -- for kk,vv in pairs(old_room_id) do
            --     print('=old_room_idold_room_idold_room_idold_room_idold_room_idold_room_idold_room_idold_room_id==================',kk)
            -- end
        end
    end
        print('===============data==================',#data)
    return data
end

-- 增加朋友圈消息   房间消息  
--uid 创建者UID fid 要推送的朋友圈id room_id 要推送到的朋友圈id game_type 游戏类型
function FRIENDS.add_msg_room( uid,fid,data ) 
    data.time = os.time()
    data.uid = uid
    local str = json.encode(data)
    print('===add_msg_room================='..str)
    local users = FRIENDS.get_fid(fid) 
    local to_uid
    if users ~= nil and users ~= false then
        for k,v in pairs(users) do 
            -- if uid ~= v['uid'] then --给房主自己发消息
                to_uid = v['uid'] 
                local rs = skynet.call("MG_SAVE", "lua", "redis_lpush", FRIENDS_KEY.key .. to_uid,str ) 
            -- end
        end
    end
end


function FRIENDS.friends_add(uid,fid) 
    local id = FRIENDS.add(tonumber(uid),tonumber(fid)) 


    return id
end
function FRIENDS.friends_get(uid)
    local  list = FRIENDS.get_uid(tonumber(uid) )  
    -- for k,v in pairs(list) do
    --     for kv,vv in pairs(v) do
    --         print(k,kv,vv) 
    --     end
    -- end
    return true,list
end

function FRIENDS.friends_msg(uid) 
    local data = FRIENDS.get_msg(tonumber(uid) ) 
    local rs = {} 
    if data ~= false and data ~= nil then
        print('=FRIENDS.friends_msg============',#data )
        for k,v in pairs(data) do   
            local stsssr = json.encode(v)
            print('=friends_msg============',k,stsssr )
            local room_id = math.ceil(v['room_id'])  
            local hall_name = skynet.call("MG_SAVE", "lua", "hall_name_hget", room_id) 
            local fd = skynet.call(hall_name, "lua", "get_rooms", room_id )
            if fd ~= nil then
                local info,room_info = skynet.call( fd, "lua", "get_users" )  
                if info then
                    local id_key = 0
                    v['name_1'] = '' 
                    v['name_2'] = ''
                    v['name_3'] = '' 
                    v['head_1'] = '' 
                    v['head_2'] = '' 
                    v['head_3'] = ''  
                    v['gameType'] = room_info['gameType'] 
                    v['ruleType'] = room_info['ruleType'] 
                    v['optionType'] = room_info['optionType'] 
                    for kk,vv in pairs(info) do 
                        if vv ~= false and vv ~= nil then  
                            id_key = id_key + 1
                            v['name_'..id_key] = vv['name']
                            v['head_'..id_key] = vv['head'] 
                        end
                    end
                    if id_key > 0 and id_key < 4 then 
                        table.insert(rs, v)
                    end
                end  
            end  
        end
    end 
    return true,rs
end

--判断uid是否是 fid 的成员
function FRIENDS.is_uid_fid(uid,fid) 
    local sql = string.format("select * from  `friends` where uid = %d and fid = %d ",
            tonumber(uid),tonumber(fid)) 
    local data = skynet.call("MG_SAVE", "lua", "mysql_get_one", sql ) 
    local rs = 0
    if data ~= false and data['flag'] > 0 then
        rs = data['flag']
    end
    return rs  
end


--在 fid 删除由uid 创建的私房消息  
function FRIENDS.del_msg(uid,fid) 
    uid = tonumber(uid) 
    local users = FRIENDS.get_fid(fid) 
    local to_uid
    if users ~= nil then
        for k,v in pairs(users) do
            if uid ~= v['uid'] then
                to_uid = v['uid'] 
                --获取uid 的msg 
                local str = skynet.call("MG_SAVE", "lua", "redis_lrange", FRIENDS_KEY.key .. to_uid,0,20 ) 
                local arr = {} 
                local now_time = os.time() - 3600 * 2 --  个小时以上数据不处理
                if str ~= nil then
                    for k,vv in pairs(str) do
                        arr = json.decode(vv)
                        if arr['time'] < now_time then
                            if k > 1 then   --删除过期数据 
                                local rs = skynet.call("MG_SAVE", "lua", "redis_ltrim", FRIENDS_KEY.key .. uid,0,k-2 ) 
                            else 
                                local rs = skynet.call("MG_SAVE", "lua", "redis_del", FRIENDS_KEY.key .. uid ) 
                            end
                            break
                        end 
                        if arr['uid'] == uid then 
                            local rs = skynet.call("MG_SAVE", "lua", "redis_lrem", FRIENDS_KEY.key .. to_uid, 0, vv ) 
                            break 
                        end  
                    end
                end
            end
        end
    end
end

return FRIENDS
